/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package skv.db.daten.entities;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import skv.db.abst.SkvEntity;
import skv.db.iface.DbHandler;

/**
 * Stellt die Benutzertabelle dar.
 * @author Alex
 */
public class Benutzer extends SkvEntity{
    private String Vorname;
    private String Nachname;

    @Override
    public String toString() {
        return getNachname()+", " + getVorname();
    }



   

    private int rollenID;

    public int getRollenID() {
        return rollenID;
    }

    public void setRollenID(int rollenID) {
        this.rollenID = rollenID;
    }
    private String loginname;
    private String password;
    

    public Benutzer(int id, DbHandler dbh) {
        
        this.id = id;
        this.dbh = dbh;
        getMyData();
    }

    
    
    public Benutzer(String Vorname, String Nachname, int rolle, String loginname, String password, DbHandler dbh) {
        this.Vorname = Vorname;
        this.Nachname = Nachname;

        this.rollenID = rolle;
        this.loginname = loginname;
        this.password = password;
        this.dbh = dbh;
        create();
    }
    public void setPassword(String password) {
        this.password = password;
        update();
    }

    public String getPassword() {
        return password;
    }
    

    public String getNachname() {
        return Nachname;
    }

    public void setNachname(String Nachname) {
        this.Nachname = Nachname;
        update();
    }

    public String getVorname() {
        return Vorname;
    }

    public void setVorname(String Vorname) {
        this.Vorname = Vorname;
        update();
    }
    public String getLoginname() {
        return loginname;
    }

    public void setLoginname(String loginname) {
        this.loginname = loginname;
        update();
    }

    @Override
    public void update() {
        
        dbh.doQuery("UPDATE `skv`.`benutzer` SET "
                + "`benName` = '"+this.Nachname+"', `benVorname` = '"+this.Vorname+"', `benRolle` = '"+this.rollenID+"', `passwort` = '"+this.password+"', "
                + "`benLogin` = '"+this.loginname+"' WHERE `benutzer`.`benID` = "+this.id+";");
        
        
    }

    @Override
    public void getMyData() {
        try {
            
           ResultSet rst = dbh.doQuery("SELECT * FROM benutzer WHERE benID = " + this.id);
           rst.next();
           this.Nachname = rst.getString("benName");
           this.Vorname = rst.getString("benVorname");
           this.loginname = rst.getString("benLogin");
           this.password = rst.getString("passwort");
           this.rollenID = rst.getInt("benRolle");
           rst.close();
           
        } catch (SQLException ex) {
            Logger.getLogger(Benutzer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void create() {
        dbh.doQuery("INSERT INTO `skv`.`benutzer` (`benID`, `benName`, `benVorname`, `benRolle`, `passwort`, `benLogin`) "
                + "VALUES (NULL, '"+this.Nachname+"', '"+this.Vorname+"', '"+this.rollenID+"', '"+this.password+"', '"+this.loginname+"');");    
        this.getLastInsertID();
         
    }

    @Override
    public boolean isUnique() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete() {
        dbh.doQuery("DELETE FROM `skv`.`benutzer` WHERE `skv`.`benutzer`.`benID`="+this.id);
        dbh.doQuery("DELETE FROM `skv`.`ben_klasse` WHERE `skv`.`ben_klasse`.`benID`="+this.id);
        dbh.doQuery("DELETE FROM `skv`.`benotungen` WHERE `skv`.`benotungen`.`schuelerid`="+this.id);
        
    }
     
}
